package com.example.controller;


import com.example.domain.Record;
import com.example.domain.Record2;
import com.example.service.RecordService;
import com.example.service.RecordService2;
import com.example.utils.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.example.utils.AjaxResult.*;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/root")

public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private RecordService2 recordService2;

    /** 药品管理 */
    @RequestMapping("/record")
    public AjaxResult Record(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                                  @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                                  @RequestParam(defaultValue = "" , value = "record") String usr, HttpServletRequest request
    ){
        int patient_id =(int) request.getSession().getAttribute("patient_id");
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Record> record = recordService.listAll(patient_id);
            System.out.println(record.toString());
            PageInfo<Record> page = new PageInfo<>(record);
            return successData(page.getTotal(), record);
        }
        map.put("usr",usr);
        List<Record> record = recordService.listAll(patient_id);
        PageInfo<Record> page = new PageInfo<>(record);
        return successData(page.getTotal(), record);
    }

    /** 药品管理 */
    @RequestMapping("/record2")
    public AjaxResult Record2(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                             @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                             @RequestParam(defaultValue = "" , value = "record2") String usr, HttpServletRequest request
    ){
//        int patient_id =(int) request.getSession().getAttribute("patient_id");
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        if (usr == null || usr.equals("")) {
            map.put("usr","");
            List<Record2> record = recordService2.listAll(1);
            System.out.println(record.toString());
            PageInfo<Record2> page = new PageInfo<>(record);
            return successData(page.getTotal(), record);
        }
        map.put("usr",usr);
        List<Record2> record = recordService2.listAll(1);
        PageInfo<Record2> page = new PageInfo<>(record);
        return successData(page.getTotal(), record);
    }

    /** 新增添加 */
    @RequestMapping("/addRecord")
    public AjaxResult addRecord(Record2 record){
        System.out.println("test1");
        int insert = recordService2.insert(record);
        if (1 == insert){
            return success("添加成功");
        }
        return error("添加失败");
    }

    /** 病例修改 */
    @RequestMapping("/modifyRecord")
    public AjaxResult modifyRecord(Record2 record){

        int update = recordService2.update(record);

        if (1 == update){
            return success("修改成功");
        }
        return error("修改失败");
    }

    @RequestMapping("/batchDelRecord")
    public AjaxResult batchDelRecord(String listStr){
        if (null != listStr && !"".equals(listStr)){
            String[] ids = listStr.split(",");
            for (String id:ids) {
                if (null != id && !"".equals(id)) {
                    System.out.println(id);
                    recordService2.delete(Integer.valueOf(id));
                }
            }
        }
        return success("删除成功");
    }

    @RequestMapping("/delRecord")
    public AjaxResult delRecord(@RequestParam(value = "id") Integer id){
        recordService2.delete(id);
        return success("删除成功");
    }

}
